package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.SystemMapper;
import com.cskaoyan.model.System;
import com.cskaoyan.model.SystemExample;
import com.cskaoyan.model.vo.ConfigOrderVO;
import com.cskaoyan.model.vo.ConfigWxVO;
import com.cskaoyan.model.vo.ExpressVO;
import com.cskaoyan.model.vo.MarketSystemVO;
import com.cskaoyan.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ：huzhouming
 * @description：实现配置管理模块具体操作
 * @date ：2022/5/6 15:23
 */
@Service
@Transactional
public class ConfigServiceImpl implements ConfigService {
    @Autowired
    SystemMapper systemMapper;

    @Override
    /**
     * @author: huzhouming
     * @description: 获取商场配置信息
     * @date: 2022/5/6
     * @param
     * @return com.cskaoyan.model.vo.MarketSystemVO
     */
    public MarketSystemVO queryMall() {
        List<System> systems = systemMapper.selectByExample(new SystemExample());
        MarketSystemVO marketSystemVO = new MarketSystemVO();
        for (System system : systems) {
            switch (system.getKeyName()) {
                case "market_mall_longitude":
                    marketSystemVO.setMarket_mall_longitude(system.getKeyValue());
                    break;
                case "market_mall_latitude":
                    marketSystemVO.setMarket_mall_latitude(system.getKeyValue());
                    break;
                case "market_mall_address":
                    marketSystemVO.setMarket_mall_address(system.getKeyValue());
                    break;
                case "market_mall_qq":
                    marketSystemVO.setMarket_mall_qq(system.getKeyValue());
                    break;
                case "market_mall_phone":
                    marketSystemVO.setMarket_mall_phone(system.getKeyValue());
                    break;
                case "market_mall_name":
                    marketSystemVO.setMarket_mall_name(system.getKeyValue());
                    break;
            }
        }
        return marketSystemVO;
    }

    @Override
    /**
     * @author: huzhouming
     * @description: 修改商场配置信息
     * @date: 2022/5/6
     * @param marketSystemVO
     * @return void
     */
    public void updateMall(MarketSystemVO marketSystemVO) {
        List<System> systems = systemMapper.selectByExample(new SystemExample());
        for (System system : systems) {
            system.setId(null);
            SystemExample systemExample = new SystemExample();
            SystemExample.Criteria criteria = systemExample.createCriteria();
            switch (system.getKeyName()) {
                case "market_mall_longitude":
                    system.setKeyValue(marketSystemVO.getMarket_mall_longitude());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_mall_latitude":
                    system.setKeyValue(marketSystemVO.getMarket_mall_latitude());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_mall_address":
                    system.setKeyValue(marketSystemVO.getMarket_mall_address());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_mall_qq":
                    system.setKeyValue(marketSystemVO.getMarket_mall_qq());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_mall_phone":
                    system.setKeyValue(marketSystemVO.getMarket_mall_phone());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_mall_name":
                    system.setKeyValue(marketSystemVO.getMarket_mall_name());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
            }
        }
    }

    @Override
    /**
     * @author: huzhouming
     * @description: 获取Express信息
     * @date: 2022/5/6
     * @param
     * @return com.cskaoyan.model.vo.ExpressVO
     */
    public ExpressVO queryExpress() {
        List<System> systems = systemMapper.selectByExample(new SystemExample());
        ExpressVO expressVO = new ExpressVO();
        for (System system : systems) {
            switch (system.getKeyName()) {
                case "market_express_freight_min":
                    expressVO.setMarket_express_freight_min(system.getKeyValue());
                    break;
                case "market_express_freight_value":
                    expressVO.setMarket_express_freight_value(system.getKeyValue());
                    break;
            }
        }
        return expressVO;
    }

    @Override
    /**
     * @author: huzhouming
     * @description: 修改Express信息
     * @date: 2022/5/6
     * @param expressVO
     * @return void
     */
    public void updateExpress(ExpressVO expressVO) {
        List<System> systems = systemMapper.selectByExample(new SystemExample());
        for (System system : systems) {
            system.setId(null);
            SystemExample systemExample = new SystemExample();
            SystemExample.Criteria criteria = systemExample.createCriteria();
            switch (system.getKeyName()) {
                case "market_express_freight_min":
                    system.setKeyValue(expressVO.getMarket_express_freight_min());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_express_freight_value":
                    system.setKeyValue(expressVO.getMarket_express_freight_value());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
            }
        }
    }

    @Override
    /**
     * @author: huzhouming
     * @description: 获取订单配置信息
     * @date: 2022/5/6
     * @param
     * @return com.cskaoyan.model.vo.ConfigOrderVO
     */
    public ConfigOrderVO queryOrder() {
        List<System> systems = systemMapper.selectByExample(new SystemExample());
        ConfigOrderVO configOrderVO = new ConfigOrderVO();
        for (System system : systems) {
            switch (system.getKeyName()) {
                case "market_order_unconfirm":
                    configOrderVO.setMarket_order_unconfirm(system.getKeyValue());
                    break;
                case "market_order_unpaid":
                    configOrderVO.setMarket_order_unpaid(system.getKeyValue());
                    break;
                case "market_order_comment":
                    configOrderVO.setMarket_order_comment(system.getKeyValue());
                    break;
            }
        }
        return configOrderVO;
    }

    @Override
    /**
     * @author: huzhouming
     * @description: 修改订单配置信息
     * @date: 2022/5/6
     * @param configOrderVO
     * @return void
     */
    public void updateOrder(ConfigOrderVO configOrderVO) {
        List<System> systems = systemMapper.selectByExample(new SystemExample());
        for (System system : systems) {
            system.setId(null);
            SystemExample systemExample = new SystemExample();
            SystemExample.Criteria criteria = systemExample.createCriteria();
            switch (system.getKeyName()) {
                case "market_order_unconfirm":
                    system.setKeyValue(configOrderVO.getMarket_order_unconfirm());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_order_unpaid":
                    system.setKeyValue(configOrderVO.getMarket_order_unpaid());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_order_comment":
                    system.setKeyValue(configOrderVO.getMarket_order_comment());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
            }
        }
    }


    @Override
    public ConfigWxVO queryWx() {
        List<System> systems = systemMapper.selectByExample(new SystemExample());
        ConfigWxVO configWxVO = new ConfigWxVO();
        for (System system : systems) {
            switch (system.getKeyName()) {
                case "market_wx_index_new":
                    configWxVO.setMarket_wx_index_new(system.getKeyValue());
                    break;
                case "market_wx_index_topic":
                    configWxVO.setMarket_wx_index_topic(system.getKeyValue());
                    break;
                case "market_wx_index_brand":
                    configWxVO.setMarket_wx_index_brand(system.getKeyValue());
                    break;
                case "market_wx_catlog_goods":
                    configWxVO.setMarket_wx_catlog_goods(system.getKeyValue());
                    break;
                case "market_wx_catlog_list":
                    configWxVO.setMarket_wx_catlog_list(system.getKeyValue());
                    break;
                case "market_wx_index_hot":
                    configWxVO.setMarket_wx_index_hot(system.getKeyValue());
                    break;
                case "market_wx_share":
                    configWxVO.setMarket_wx_share(system.getKeyValue());
                    break;
            }
        }
        return configWxVO;
    }

    @Override
    /**
     * @author: huzhouming
     * @description: 修改小程序订单信息
     * @date: 2022/5/6
     * @param configWxVO
     * @return void
     */
    public void updateWx(ConfigWxVO configWxVO) {
        List<System> systems = systemMapper.selectByExample(new SystemExample());
        for (System system : systems) {
            system.setId(null);
            SystemExample systemExample = new SystemExample();
            SystemExample.Criteria criteria = systemExample.createCriteria();
            switch (system.getKeyName()) {
                case "market_wx_index_new":
                    system.setKeyValue(configWxVO.getMarket_wx_index_new());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_wx_index_topic":
                    system.setKeyValue(configWxVO.getMarket_wx_index_topic());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_wx_share":
                    system.setKeyValue(configWxVO.getMarket_wx_share());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_wx_index_brand":
                    system.setKeyValue(configWxVO.getMarket_wx_index_brand());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_wx_catlog_goods":
                    system.setKeyValue(configWxVO.getMarket_wx_catlog_goods());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_wx_catlog_list":
                    system.setKeyValue(configWxVO.getMarket_wx_catlog_list());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
                case "market_wx_index_hot":
                    system.setKeyValue(configWxVO.getMarket_wx_index_hot());
                    system.setUpdateTime(new Date());
                    criteria.andKeyNameEqualTo(system.getKeyName());
                    systemMapper.updateByExampleSelective(system, systemExample);
                    break;
            }
        }
    }

}